package org.vectrics.recruiter.struts.admin;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Logger;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class JobCandidateSetSourceAction extends VectricsStrutsAction {
    private transient static Logger log = Logger.getLogger(JobCandidateSetSourceAction.class);
	public static String CURRENT_JOB_KEY = "current-job";
    //public static String ACTION_NAME = "save_job";
    public static String JOB_PARAM = "jobId";    
    public static String ACTIVATE_PARAM = "activate";    
    
    
    //private boolean newJob = false;
	//private RemoteUserProvider userManager = new RemoteUserProvider();
	//private JobManager jobManager = new JobManager();
   

    public JobCandidateSetSourceAction() {
    	super();
    }


	public String doExecute(VectricsActionData actionData)	throws JspException {
		Request request = actionData.getRequest();
		
		Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(ThreadProperties.getDomain(), request);
		
        log.debug("execute() called, reading http parameters");
        String jobCandidateIdParam = request.getParameter("jobCandidateId");
        Long jobCandidateId = null;
        if (jobCandidateIdParam != null)
            jobCandidateId = new Long(jobCandidateIdParam);
        
        Person sourcePerson = CoreServiceLocator.getPersonService().findPersonById(new Long(request.getParameter("personId")));
        
        JobCandidate jobCandidate = RecruitServiceLocator.getJobProvider().findJobCandidateById(jobCandidateId);
        jobCandidate.setSource(sourcePerson);
        
        CandidateInfo candidateInfo = jobCandidate.getCandidate();
        if (candidateInfo.getSource() == null) {
        	candidateInfo.setSource(sourcePerson);
        }

        RecruitServiceLocator.getJobProvider().updateJobCandidate(jobCandidate);
        
		return ("success");
    }


    
}
